<template>
<!--  返回组建（自主添加）-->
  <van-nav-bar
      title="登录"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
  />

  <van-form @submit="handleLogin">
    <van-cell-group inset>
      <van-field
          v-model="user.username"
          label="用户名"
          placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名',trigger: 'onChange'}]"
      />
      <van-field
          v-model="user.password"
          type="password"
          name="密码"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码',trigger: 'onChange'},
          {pattern: /^[a-z]{6,10}$/,message:'密码格式错误(密码格式：6-12位小写字母)',trigger: 'onChange'}]"
      />
    </van-cell-group>
    <div style="margin: 16px;">
      <van-button round block type="primary" native-type="submit">
        提交
      </van-button>
    </div>
  </van-form>

</template>

<script>
//引包红色确认框
import {showDialog} from 'vant';
//引包绿色下拉登录成功
import {showNotify} from 'vant';

export default {
  name: "login",
  // v3钩子函数（返回）
  setup(){
    const onClickLeft = () => history.back();
    return {
      onClickLeft,
    }
  },
  data(){
    return{
      user: {
        username: '',
        password: ''
      },
      redirect: ''
    }
  },
  methods: {
    handleLogin() {
      console.log(1)
      // 发送网络请求给服务器端
      // TODO 要不要加密
      this.$axios.post('/ele_server/user/login', this.user).then(({data: res}) => {
        if (res.status == 0) {
          //红色确认弹框
          showDialog({
            message: res.message,
            theme: 'round-button'
          })
          // this.$router.back();
          this.$router.push(this.redirect)
          //绿色下拉登录成功
          // showNotify({ type: 'success', message: res.message });
          // this.$router.push(this.redirect)
        } else {
          alert(res.message)
        }
      })
    }
  },
  created() {

    this.redirect = this.$route.query.redirect ? this.$route.query.redirect : '/business/list'
  }
}
</script>


<style scoped lang="stylus">

</style>